package yns.springboot.guide.demo.data.db.mybatis.dao;

import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectKey;
import org.apache.ibatis.annotations.Update;

import yns.springboot.guide.demo.entity.UserEntity;

public interface UserMapperAnnotationByMybatis {
//	#和$的区别
//	
//  This example creates a prepared statement, something like select * from teacher where name = ?;
//	@Select("Select * from teacher where name = #{name}")
//	Teacher selectTeachForGivenName(@Param("name") String name);
//
//  This example creates n inlined statement, something like select * from teacher where name = 'someName';
//	@Select("Select * from teacher where name = '${name}'")

	// 推荐通过静态变量的方式，先写好SQL
	public static String SelectSQL = "SELECT ID, Name, Sex,Age,Describe FROM T_USER";
	public static String SelectKey = "SELECT (SELECT max(Id) FROM T_USER)+1";

	@Select(SelectSQL)
//	@Results({@Result(property = "nickName", column = "nick_name"，javaType = UserSexEnum.class) })  
	// 列名和属性名不一样的时候可以映射和类型转换。
	// 推荐在Entity中的列名和属性名（包括字段类型）保持一致，需要做转换的，在service中完成（推荐业务Bean和Entity分开）。
	public List<UserEntity> getUser();

	@Insert("INSERT INTO T_USER(ID,Name,Sex,Age,Describe) VALUES(#{id},#{name}, #{sex}, #{age}, #{describe})")
	@SelectKey(statement = SelectKey, keyProperty = "id", keyColumn = "ID", before = true, resultType = int.class)
	public void addUser(UserEntity userEntity);

	@Delete("DELETE FROM T_USER WHERE id = #{id}")
	public void deleteUser(int id);

	@Update("UPDATE T_USER SET Describe=#{describe} WHERE id =#{id}")
	public void updateUser(UserEntity userEntity);

}
